package com.mall.server.controller.ums;

import com.mall.model.domain.UmsMenu;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.CommonResult;
import com.mall.model.vo.UmsMenuVo;
import com.mall.server.service.ums.UmsMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/menu")
public class UmsMenuController {

    @Autowired
    private UmsMenuService umsMenuService;

    /**
     * 添加后台菜单
     */
    @PostMapping("/create")
    public CommonResult saveMenu(@RequestBody UmsMenu umsMenu) {

        umsMenuService.saveMenu(umsMenu);

        return CommonResult.success(null);
    }

    /**
     * 分页查询后台菜单
     */
    @GetMapping("/list/{parentId}")
    public CommonResult list(@PathVariable("parentId") Integer parentId
            , @RequestParam(defaultValue = "1") Integer pageNum
            , @RequestParam(defaultValue = "10") Integer pageSize) {

        CommonPage commonPage = umsMenuService.findById(parentId, pageNum, pageSize);
        return CommonResult.success(commonPage);

    }

    /**
     * 根据ID删除后台菜单
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable("id") Integer id) {

        umsMenuService.deleteUmsMenu(id);

        return CommonResult.success(null);
    }

    /**
     * 树形结构返回所有菜单列表
     */
    @GetMapping("/treeList")
    public CommonResult treeList() {

        List<UmsMenuVo> vo = umsMenuService.findTreeList();

        return CommonResult.success(vo);
    }

    /**
     * 修改后台菜单
     */
    @PostMapping("/update/{id}")
    public CommonResult updateMenu(@PathVariable("id") Integer id, @RequestBody UmsMenu umsMenu) {

        umsMenuService.updateMenu(id, umsMenu);

        return CommonResult.success(null);
    }

    /**
     * 修改菜单显示状态
     */
    @PostMapping("/updateHidden/{id}")
    public CommonResult updateHiddenMenu(@PathVariable("id") Integer id, Integer hidden) {

        umsMenuService.updateHiddenMenu(id, hidden);

        return CommonResult.success(null);
    }


    /**
     * 根据ID获取菜单详情
     */
    @GetMapping("/{id}")
    public CommonResult findById(@PathVariable("id") Integer id) {

        UmsMenu umsMenu = umsMenuService.findId(id);

        return CommonResult.success(umsMenu);
    }

}
